package 尚硅谷juc大厂.test12;

import java.util.concurrent.TimeUnit;

public class Test12 {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                boolean interrupted = Thread.currentThread().isInterrupted();
                if (interrupted) {
                    System.out.println("程序结束......");
                    break;
                }else {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        /**
                         * 解决阻塞中的线程被打断会被立即唤醒，并且抛出InterruptedException异常，但是此线程不会停止的问题
                         * 在catch (InterruptedException e)块中再掉一次Thread.currentThread().interrupt();
                         */
                        Thread.currentThread().interrupt();
                        e.printStackTrace();
                    }
                    System.out.println("t1线程运行输出.......");
                }

            }
        }, "t1");
        t1.start();

        TimeUnit.SECONDS.sleep(3);
        /**
         * 调用打断
         */
        t1.interrupt();
    }
}
